package com.ys.controller.api.sys;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ys.service.business.TpProductBasicService;
import com.ys.service.finance.*;
import com.ys.service.overseas.TosChannelStockService;
import com.ys.service.overseas.TosProductActiveService;
import com.ys.service.overseas.TosProductBasisService;
import com.ys.service.overseas.TosPurchasePlanItemService;
import com.ys.service.purchase.TcgOfflinePurchaseInStorageService;
import com.ys.service.storage.TccLogisticsComprehensiveOrderService;
import com.ys.service.storage.TccLogisticsReconcileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ys.config.ImportConfig;
import com.ys.config.MenuId;
import com.ys.config.MyConfig;
import com.ys.entity.sys.YsCol;
import com.ys.enume.sys.YsColEnum;
import com.ys.service.business.TpBoutiqueService;
import com.ys.service.business.TpBoutiqueSiteDetailService;
import com.ys.service.business.TpProductBqService;
import com.ys.service.kefu.TpGaugeMaintenanceService;
import com.ys.service.sys.CommonService;
import com.ys.util.ImportExcelUtils;
import com.ys.util.shiro.ShiroUtils;
import com.ys.vo.R;

import lombok.AllArgsConstructor;
import lombok.Data;

@Controller
@RequestMapping("/api/inter/common")
public class ApiInterCommonController {
	
//	private static final Logger logger = Logger.getLogger(ApiAdminLoginController.class);

	@Autowired
	private CommonService commonService;

	@Autowired
	private TpBoutiqueSiteDetailService tpBoutiqueSiteDetailService;

	@Autowired
	private TpBoutiqueService tpBoutiqueService;

	@Autowired
	private TpProductBqService tpProductBqService;

	@Autowired
	private TpGaugeMaintenanceService tpGaugeMaintenanceService;

	@Autowired
	private TcwFinancesApiDataService tcwFinancesApiDataService;

	@Autowired
	private TcwElectronicInvoiceService tcwElectronicInvoiceService;

	@Autowired
	private TcwExpenseSettlementService tcwExpenseSettlementService;

	@Autowired
	private TcwExpenseAllocationService tcwExpenseAllocationService;

	@Autowired
	private TcwEntryApplyService tcwEntryApplyService;

	@Autowired
	private TcwPaymentApplyService tcwPaymentApplyService;

	@Autowired
	private TcwRoleCostApplyService tcwRoleCostApplyService;

	@Autowired
	private TcgOfflinePurchaseInStorageService tcgOfflinePurchaseInStorageService;

	@Autowired
	private TosProductActiveService tosProductActiveService;

	@Autowired
	private TosChannelStockService tosChannelStockService;

	@Autowired
	private TosProductBasisService tosProductBasisService;

	@Autowired
	private TosPurchasePlanItemService tosPurchasePlanItemService;

	@Autowired
	private TccLogisticsComprehensiveOrderService tccLogisticsComprehensiveOrderService;

	@Autowired
	private TccLogisticsReconcileService tccLogisticsReconcileService;


	@Autowired
	private TpProductBasicService tpProductBasicService;

	
	/**
	 * 在表格上编辑
	 */
	@PostMapping("/editOnTable")
    @ResponseBody
	public R editOnTable(Model model, HttpServletRequest request, HttpServletResponse response){
		
		String loginUserId = ShiroUtils.getUserId();
		if (!"admin".equals(loginUserId)) {
			return R.error("非管理员无法操作");
		}
		
		return commonService.editOnTable(request);
	}

	/**
	 * 生成导入模板文件
	 */
	@PostMapping("/generateImportTemplate")
	@ResponseBody
	public R generateImportTemplate(HttpServletRequest request,
			Long menuId) {

		if (menuId == null) {
			return R.error("菜单不能为空");
		}

		try {
	        
	    	List<YsCol> listField = new YsCol()
	    			.setMenuId(menuId)
	    			.setImportState(YsColEnum.ImportState.YES.value)
	    			.setOrderby("A.ImportSortNo asc")
	    			.queryList();
	    	
	    	String filePreName = ImportConfig.ID_FILENAME_MAP.get(menuId);
	    	String fileName = filePreName + ".xlsx";
	    	
	        ImportExcelUtils util = new ImportExcelUtils();
	        // 生成模板文件到指定目录下
	        util.exportExcel(listField, filePreName);
	        
	        String url = MyConfig.getUrlUploadAdmin() + "/" + fileName;
	        GenerateImportTemplateDto dto = new GenerateImportTemplateDto(url, fileName);
			return R.ok(dto);
		} catch (Exception e) {
			e.printStackTrace();
			return R.error();
		}
	}
	/**
	 * 生成导入模板
	 * @author Jason
	 *
	 */
	@Data
	@AllArgsConstructor
	public class GenerateImportTemplateDto {
		private String url; // 链接地址
		private String fileName; // 文件名称
	}

	/**
	 * 批量导入
	 */
	@PostMapping("/importBatch")
	@ResponseBody
	public R importBatch(HttpServletRequest request,
			Long menuId, String rows){
		try {

			System.out.println(rows);
			if (menuId.equals(MenuId.TP_BoutiqueSiteDetail)) {//精品库站点信息批量导入
				return tpBoutiqueSiteDetailService.importInfo(rows,menuId);
			}else if (menuId.equals(MenuId.TP_Boutique)) {//精品库信息批量导入
				return tpBoutiqueService.importInfo(rows,menuId);
			}else if (menuId.equals(MenuId.TP_ProductBqSp)) {//精品库信息批量导入
				return tpProductBqService.importInfo(rows,menuId);
			}else if (menuId.equals(MenuId.TP_GaugeMaintenance)) {//箱规维护批量导入
				return tpGaugeMaintenanceService.importInfo(rows,menuId);
			}else if (menuId.equals(MenuId.TCW_FinancesEventGroups)) {//财务事件批量导入
				return tcwFinancesApiDataService.importInfo(rows,menuId);
			}else if (menuId.equals(MenuId.TCW_ElectronicInvoice)) {//运营电子发票开票单批量导入
				return tcwElectronicInvoiceService.importInfo(rows,menuId);
			}else if (menuId.equals(MenuId.TCW_ExpenseSettlement)) {//费用结算单批量导入
				return tcwExpenseSettlementService.importInfo(rows,menuId);
			}else if (menuId.equals(MenuId.TCW_ExpenseAllocation)) {//费用划拨单批量导入
				return tcwExpenseAllocationService.importInfo(rows,menuId);
			}else if (menuId.equals(MenuId.TCW_ExpenseAllocation_ByManage)) {//费用划拨单批量导入
				return tcwExpenseAllocationService.importInfo(rows,menuId);
			}else if (menuId.equals(MenuId.TCW_EntryApply)) {//费用划拨单批量导入
				return tcwEntryApplyService.importInfo(rows,menuId);
			}else if (menuId.equals(MenuId.TCW_PaymentApply)) {//付款申请单
				return tcwPaymentApplyService.importInfo(rows,menuId);
			}else if (menuId.equals(MenuId.TCW_RoleCostApply)) {//职能费用申请
				return tcwRoleCostApplyService.importInfo(rows,menuId);
			}else if (menuId.equals(MenuId.TCG_OfflinePurchaseInStorage)) {//线上采购入库
				return tcgOfflinePurchaseInStorageService.importInfo(rows,menuId);
			}else if (menuId.equals(MenuId.TOS_ProductActive)) {//HW商品listing库
				return tosProductActiveService.importInfo(rows,menuId);
			}else if (menuId.equals(MenuId.TOS_ChannelStock)) {//HW商品渠道库存
				return tosChannelStockService.importInfo(rows,menuId);
			}else if (menuId.equals(MenuId.TOS_ProductBasis)) {//HW商品库
				return tosProductBasisService.importInfo(rows,menuId);
			}else if (menuId.equals(MenuId.TOS_PurchasePlanItemJs)) {//追踪编码订单导入
				return tosPurchasePlanItemService.importInfoDd(rows,menuId);
			}else if (menuId.equals(MenuId.TOS_PurchasePlanItemFh)) {//追踪编码商品编码导入
				return tosPurchasePlanItemService.importInfoSp(rows,menuId);
			}else if (menuId.equals(MenuId.TP_ProductBq)) {//商品备案申请优品批量导入
				return tpProductBqService.importInfoYp(rows,menuId);
			}else if (menuId.equals(MenuId.TCC_LogisticsComprehensive_Order)) {//物流订单 - 物流信息导入
				return tccLogisticsComprehensiveOrderService.locationImport(rows,menuId);
			}else if (menuId.equals(MenuId.TCC_LOGISTICSRECONCILE)) {//服务商物流费用导入
				return tccLogisticsReconcileService.costImport(rows,menuId);
			}else if (menuId.equals(MenuId.TCC_LOGISTICSRECONCILESERVICEGS)) {//服务商关税费用导入
				return tccLogisticsReconcileService.gsImport(rows,menuId);
			}else if (menuId.equals(MenuId.TCC_LOGISTICSRECONCILEDZSM)) {//对账说明导入
				return tccLogisticsReconcileService.dZsmImport(rows,menuId);
			}else if (menuId.equals(MenuId.TS_BatchImportLog_TpProductBasis)) {// 批量导入详情-商品库
				return tpProductBasicService.importInfo(rows,menuId);
			}else if (menuId.equals(MenuId.TS_BatchImportLog_TpProductBasisSub)) {// 批量导入详情-商品库子件
				return tpProductBasicService.importInfoBySub(rows,menuId);
			}
			return R.ok();


		} catch (Exception e) {
			e.printStackTrace();
			return R.error();
		}

	}
}
